<template>
  <t-dialog
    :header="`${header}-附件预览`"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    :confirmBtn="null"
    width="80%"
    top="5%"
    v-if="isDialog"
  >
    <div style="height: 65vh">
      <t-tabs v-model="tabValue" placement="left">
        <t-tab-panel  v-for="(data,index) in projectFileData" :label="data.c1"
                     :value="data.moduleKey" :key="index">
          <div style="flex: 1">
            <div :id="item.id" v-for="(item, index) in data.fileLists" :key="index">
              <t-divider><a class="t-button-link" target="_blank" :href="item.url">
                {{ item.name }}
              </a></t-divider>
              <pdf :width="1000" v-if="item.url.includes('.pdf')" :source="item.url" :number="1"></pdf>
              <div v-else-if="['.doc','.docx'].includes(item.url)">
                不支持预览
              </div>
              <t-image

                v-else
                :src="item.url"
              ></t-image>
            </div>
          </div>
          <!--       <t-anchor container="#anchor-container" :affixProps="{ offsetTop: 200 }">-->
          <!--         <t-anchor-item @click="anchor(item.id)" target="_parent" :href="`#${item.id}`" v-for="(item,index) in fileLists" :title="item.name" :key="index"></t-anchor-item>-->
          <!--       </t-anchor>-->
        </t-tab-panel>
        <t-tab-panel v-if="!['zjb','xqzjb'].includes(achievementType)" value="2" label="评价信息">
          <evaluate-detail v-if="openPJ" :objId="objId" :achievement-type="achievementType"></evaluate-detail>
          <div v-else style="height: 65vh;width: 100%;display: flex;justify-content: center;align-items: center;font-weight: bold;font-size: 50px;color: #ccc;">待填写评价信息...</div>
        </t-tab-panel>
        <t-tab-panel v-if="!['zjb','xqzjb'].includes(achievementType)" value="3" label="推广信息">
          <extend-detail v-if="openTG" :objId="objId" :achievement-type="achievementType"></extend-detail>
          <div v-else style="height: 65vh;width: 100%;display: flex;justify-content: center;align-items: center;font-weight: bold;font-size: 50px;color: #ccc;">待填写推广信息...</div>
        </t-tab-panel>
      </t-tabs>
    </div>
  </t-dialog>
  <div style="height: 80vh" v-else>
    <t-tabs v-model="tabValue" placement="left">
      <t-tab-panel v-for="(data,index) in projectFileData" :label="data.c1"
                   :value="data.moduleKey" :key="index">
        <div style="flex: 1;display: flex;justify-content: center">
          <div :id="item.id" v-for="(item, index) in data.fileLists" :key="index">
              <t-divider><a class="t-button-link" target="_blank" :href="item.url">
                {{ item.name }}
              </a></t-divider>
            <pdf :width="1000" v-if="item.url.includes('.pdf')" :source="item.url" :number="1"></pdf>
            <div v-else-if="['.doc','.docx'].includes(item.url)">
              不支持预览
            </div>
            <t-image

              v-else
              :src="item.url"
            ></t-image>
          </div>
        </div>
        <!--       <t-anchor container="#anchor-container" :affixProps="{ offsetTop: 200 }">-->
        <!--         <t-anchor-item @click="anchor(item.id)" target="_parent" :href="`#${item.id}`" v-for="(item,index) in fileLists" :title="item.name" :key="index"></t-anchor-item>-->
        <!--       </t-anchor>-->
      </t-tab-panel>
      <t-tab-panel v-if="!['zjb','xqzjb'].includes(achievementType)" value="2" label="评价信息">
        <evaluate-detail v-if="openPJ" :objId="objId" :achievement-type="achievementType"></evaluate-detail>
        <div v-else style="height: 65vh;width: 100%;display: flex;justify-content: center;align-items: center;font-weight: bold;font-size: 50px;color: #ccc;">待填写评价信息...</div>
      </t-tab-panel>
      <t-tab-panel v-if="!['zjb','xqzjb'].includes(achievementType)" value="3" label="推广信息">
        <extend-detail v-if="openTG" :objId="objId" :achievement-type="achievementType"></extend-detail>
        <div v-else style="height: 65vh;width: 100%;display: flex;justify-content: center;align-items: center;font-weight: bold;font-size: 50px;color: #ccc;">待填写推广信息...</div>
      </t-tab-panel>
    </t-tabs>
  </div>
</template>

<script>
import {getFiles} from "@/api/common";
import evaluateDetail from "../evaluate/components/evaluate-detail.vue";
import extendDetail from "../extend/components/extend-detail.vue";

export default {
  name: "file-review",
  components: {
    evaluateDetail,
    extendDetail
  },
  props: {
    objId: [String, Number],
    header: String,
    achievementType: String,
    isDialog: {
      type: Boolean,
      default: true
    },
    // projectFileData: {
    //   type: Array,
    //   default: () => []
    // },
    reviewOpen: {
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      visible: true,
      tabValue: '',
      fileLists: [],
      projectFileData: [],
      openPJ: false,
      openTG: false,
    }
  },
  watch: {
    objId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj(val);
        }
      }
    },
  },
  created() {
    // this.tabValue = this.projectFileData[0].moduleKey;
    this.checkPJ();
    this.checkTG();
  },
  methods: {
    checkPJ(){
      this.$api.cgzh.evaluate.getByAchievementTypeAndId({
        achievementType:this.achievementType,
        achievementId:this.objId,
      }).then(response => {
        if(response.data){
          this.openPJ = true
        }
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    checkTG(){
      this.$api.cgzh.extend.getByAchievementTypeAndId({
        achievementType:this.achievementType,
        achievementId:this.objId,
      }).then(response => {
        if(response.data){
          this.openTG = true
        }
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    getObj() {
      if (this.achievementType === '1') {
        this.projectFileData = [
          {
            id: 1,
            c1: "正文",
            c2: "是",
            moduleKey: "thesisZW",
            fileIds: [],
            fileLists: [],
            require: true,
          },
        ]
      } else if (this.achievementType === '2') {
        this.projectFileData = [
          {
            id: 1,
            c1: "专利证书",
            c2: "是",
            moduleKey: "patentZS",
            fileIds: [],
            fileLists: [],
          },
        ]

      } else if (this.achievementType === '3') {
        this.projectFileData = [
          {
            id: 1,
            c1: "著作",
            c2: "是",
            moduleKey: "treatiseZZ",
            fileIds: [],
            fileLists: [],
            require: true,
          },
        ]
      } else if (this.achievementType === '4') {
        this.projectFileData = [
          {
            id: 1,
            c1: "软件著作权证书",
            c2: "是",
            moduleKey: "copyrightZS",
            fileIds: [],
            fileLists: [],
            require: true,
          },
        ]
      } else if (this.achievementType === '5') {
        this.projectFileData =  [
          {
            id: 1,
            c1: "获奖证书",
            c2: "是",
            moduleKey: "awardsZS",
            fileIds: [],
            fileLists: [],
            require: true,
          },
        ]
      } else if (this.achievementType === '6') {
        this.projectFileData = []
      } else if (this.achievementType === '7') {
        this.projectFileData = [
          {
            id: 1,
            c1: "工法附件",
            c2: "是",
            moduleKey: "gfFJ",
            fileIds: [],
            fileLists: [],
            require: true,
          },
        ]
      } else if (this.achievementType === '8') {
        this.projectFileData = [
          {
            id: 1,
            c1: "相关附件",
            c2: "是",
            moduleKey: "otherFJ",
            fileIds: [],
            fileLists: [],
            require: true,
          },
        ]
      } else if (this.achievementType === '9') {
        this.projectFileData = [
          {
            id: 1,
            c1: "软件著作权证书",
            c2: "是",
            moduleKey: "copyrightZS",
            fileIds: [],
            fileLists: [],
            require: true,
          },
        ]
      }else if (this.achievementType === 'zjb') {
        this.projectFileData = [
          {
            id: 1,
            c1: "成果征集表",
            c2: "是",
            fileIds: [],
            fileLists: [],
            moduleKey: "projectCGZJB",
            require: true,
          },
        ]
      }else if (this.achievementType === 'xqzjb') {
        this.projectFileData = [
          {
            id: 1,
            c1: "需求征集表",
            c2: "是",
            fileIds: [],
            fileLists: [],
            moduleKey: "projectXQZJB",
            require: true,
          },
        ]
      }
      this.tabValue = this.projectFileData[0].moduleKey;
      getFiles(this.objId).then((res) => {
        const files = {}
        res.data.forEach((item) => {
          if (!files[item.moduleKey]) {
            files[item.moduleKey] = []
          }
          files[item.moduleKey].push({
            name: item.fileName,
            url: item.fullFilePath,
            id: item.id,
            filePath: item.filePath
          })
        })
        this.projectFileData.forEach((val, index) => {
          this.projectFileData[index].fileLists = files[val.moduleKey] || [];
          this.projectFileData[index].fileIds = files[val.moduleKey]?.map(val => val.id) || [];
          this.fileLists.push(...(files[val.moduleKey] || []))
        })
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
    anchor(id) {
      const target = document.getElementById(id);
      if (target) {
        target.scrollIntoView();
      }
    }
  }
}
</script>

<style lang="less" scoped>
.t-tab-panel {
  padding: 10px;
  overflow: scroll;
  height: 65vh;
  display: flex;
}
</style>
